package com.demo;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.validators.RequiredStringValidator;

public class RegisterAction  extends ActionSupport  implements ModelDriven<RegisterBean>,Preparable{

	
	
	
	RegisterBean bean=new RegisterBean();
    
    
	public String execute() {

		System.out.println("execute mehtod is called");
		System.out.println(bean.getUserName());
		System.out.println(bean.getPassword());
		System.out.println(bean.getImage());
		return "success";
	}

	
	@Override
	public void validate() {
	
		System.out.println("validated method is called");
		  
		if(null==bean.getUserName() || bean.getUserName().trim().length()==0){
			
			addFieldError("userName", getText("string.blank"));//
			
		}else {
			//check in db whether username already exist or not.
			
			 if(bean.getUserName().equals("rajeevshukla")){
			
				 addFieldError("userName","UserName already exist in database");
				 
			 }
			
			
		}
		
		if (null==bean.getPassword() || bean.getUserName().trim().length()==0){
			addFieldError("password", getText("string.blank"));
			
			
		}
		
		if(bean.getAge()<=0){
			addFieldError("age", getText("integer.blank"));
		}
	
	}
	

	@Override
	public RegisterBean getModel() { //this method will be called by modelDriven interceptor during preprocessing.
		
		 System.out.println("get model method is called");
		return bean;
		
	}

	@Override
	public void prepare() throws Exception {
	 
		 //open database connection here..
		System.out.println("prepaire method is called");
	}
	
}
